﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.PowerCommand;
using MediatR;
using SqlSugar;
using static NPOI.HSSF.Util.HSSFColor;

namespace B.S.Filtration.Write.Api.Application.CommandHander.PowerCommandHander
{
    public class DelPowerCommandHander : IRequestHandler<DelPowerCommand, APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public DelPowerCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(DelPowerCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var list = await db.Queryable<Power>().FirstAsync(x => x.PowerId == request.PowerId);
            list.Del = true;

            result.data = await db.Updateable<Power>(list).ExecuteCommandAsync();

            if (result.data > 0)
            {
                result.code = APIResultCode.success;
                result.msg = "删除成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "删除失败";
            }

            return result;
        }
    }
}
